package com.zaofan.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Prepaid;
import com.zaofan.service.PrepaidService;
import com.zaofan.utils.ExcelUtils;
import com.zaofan.utils.JsonUtils;

@WebServlet("/prepaid")
public class PrepaidController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private PrepaidService prepaidService = new PrepaidService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<Prepaid> prepaids = prepaidService.findAll(params);
			Long count = prepaidService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", prepaids, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String num_s = request.getParameter("num");
			String money_s = request.getParameter("money");
			String name = request.getParameter("name");
			int num = Integer.valueOf(num_s);
			BigDecimal money = new BigDecimal(money_s);
			List<Prepaid> datas = prepaidService.createPrepaid(num, money, name);
			ExcelUtils.exportPrepaidDatas(datas, request, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void failurePrepaid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			prepaidService.failurePrepaid(id_s);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

}
